/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package xq.gwt.ui.test;

import java.util.ArrayList;
import java.util.HashMap;

import xq.gwt.ui.widgets.feedback.DefaultFeedbackPresenter;

import com.smartgwt.client.types.TreeModelType;
import com.google.gwt.user.client.ui.Widget;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.grid.CellFormatter;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.CellClickEvent;
import com.smartgwt.client.widgets.grid.events.CellClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.HStack;
import com.smartgwt.client.widgets.tree.TreeGrid;
import com.smartgwt.client.widgets.tree.Tree;
import com.smartgwt.client.widgets.tree.TreeGridField;
import com.smartgwt.client.widgets.tree.TreeNode;


/**
 *
 * @author admin
 */
public class WidgetTester {
    private HLayout myHlayout = new HLayout();
    private TextItemPropertyViewTest txtViewTest;
    private MemoryLeakTest memLeakTest;
    private ArrayList<TreeNode> treeNodes = new ArrayList<TreeNode>();
    private HStack displayStack = new HStack();
    private HashMap<String, UserAction> actionMap = new HashMap<String, UserAction>();
    
    

    public WidgetTester(){                
    	initTestActions();
    	Canvas navTree = createNavigationTree();        
        myHlayout.setMembers(navTree,displayStack);        
        myHlayout.setWidth("100%");
        myHlayout.setHeight("100%");        
        
    }

    private void initTestActions(){
    	UserAction showTextItemPropertyPresenter = new UserAction() {
			
			@Override
			public String getName() {				
				return "TextPresenter";
			}
			
			@Override
			public String getCaption() {				
				return "Text Presenter";
			}
			
			@Override
			public void execute() {
				if (txtViewTest!= null){					
					txtViewTest.getRidOffAll();
					txtViewTest = null;
				}
				txtViewTest = new TextItemPropertyViewTest();
				txtViewTest.getCanvas().markForRedraw();
				showWidget(txtViewTest.getCanvas());
				
			}
		};
		
		actionMap.put(showTextItemPropertyPresenter.getName(), showTextItemPropertyPresenter);
    	TreeNode node = new ActionTreeNode(showTextItemPropertyPresenter.getName(),showTextItemPropertyPresenter.getCaption());
    	treeNodes.add(node);
    	
         UserAction memLeakPresenter = new UserAction() {
			
			@Override
			public String getName() {				
				return "MemLeakPresenter";
			}
			
			@Override
			public String getCaption() {				
				return "Memory leak";
			}
			
			@Override
			public void execute() {
				if (memLeakTest!= null){					
					memLeakTest.getRidOffAll();					
				}
				memLeakTest = new MemoryLeakTest();
				
				showWidget(memLeakTest.getCanvas());
				
			}
		};
		
		actionMap.put(memLeakPresenter.getName(), memLeakPresenter);
    	TreeNode node0 = new ActionTreeNode(memLeakPresenter.getName(),memLeakPresenter.getCaption());
    	treeNodes.add(node0);
    	
    	UserAction showDatePresenter = new UserAction() {
			
			@Override
			public String getName() {				
				return "showDatePresenter";
			}
			
			@Override
			public String getCaption() {				
				return "Show Date Presenter";
			}
			
			@Override
			public void execute() {
				DateItemPropertyPresenterTest datePresenter = new DateItemPropertyPresenterTest();
				showWidget(datePresenter.getCanvas());
				
			}
		};
		actionMap.put(showDatePresenter.getName(), showDatePresenter);
    	TreeNode node1 = new ActionTreeNode(showDatePresenter.getName(),showDatePresenter.getCaption());
    	treeNodes.add(node1);
    	
    	UserAction showListPresenter = new UserAction() {
			
			@Override
			public String getName() {				
				return "showListPresenter";
			}
			
			@Override
			public String getCaption() {				
				return "Show List Presenter";
			}
			
			@Override
			public void execute() {
								
				PropertyListPresenterTest listPresenterTest = new PropertyListPresenterTest();
				showWidget(listPresenterTest.getCanvas());
				
				
			}
		};
		actionMap.put(showListPresenter.getName(), showListPresenter);
    	TreeNode node2 = new ActionTreeNode(showListPresenter.getName(),showListPresenter.getCaption());
    	treeNodes.add(node2);
    	
    	UserAction showCommandPresenter = new UserAction() {
			
			@Override
			public String getName() {				
				return "showCommandPresenter";
			}
			
			@Override
			public String getCaption() {				
				return "show CommandP resenter";
			}
			
			@Override
			public void execute() {
								
				ButtonCommandPresenterTest presenterTest = new ButtonCommandPresenterTest();
				showWidget(presenterTest.getCanvas());
				
				
			}
		};
		actionMap.put(showCommandPresenter.getName(), showCommandPresenter);
    	TreeNode node3 = new ActionTreeNode(showCommandPresenter.getName(),showCommandPresenter.getCaption());
    	treeNodes.add(node3);
    	
    	UserAction showEntityPresenter = new UserAction() {
			
			@Override
			public String getName() {				
				return "showEntityPresenter";
			}
			
			@Override
			public String getCaption() {				
				return "show Entity Presenter";
			}
			
			@Override
			public void execute() {
								
				FormEntityPresenterTest presenterTest = new FormEntityPresenterTest();
				showWidget(presenterTest.getCanvas());
				
				
			}
		};
		actionMap.put(showEntityPresenter.getName(), showEntityPresenter);
    	TreeNode node4 = new ActionTreeNode(showEntityPresenter.getName(),showEntityPresenter.getCaption());
    	treeNodes.add(node4);
    	
    	UserAction navigationPresenter = new UserAction() {			
			@Override
			public String getName() {				
				return "navigationPresenter";
			}
			
			@Override
			public String getCaption() {				
				return "navigation Presenter";
			}
			
			@Override
			public void execute() {								
				NavigationPresenterTest presenterTest = new NavigationPresenterTest();
				showWidget(presenterTest.getCanvas());				
			}
		};
		actionMap.put(navigationPresenter.getName(), navigationPresenter);
    	TreeNode node5 = new ActionTreeNode(navigationPresenter.getName(),navigationPresenter.getCaption());
    	treeNodes.add(node5);
    	
    	UserAction entityListPresenter = new UserAction() {			
			@Override
			public String getName() {				
				return "entityListPresenter";
			}
			
			@Override
			public String getCaption() {				
				return "Entity List Presenter";
			}
			
			@Override
			public void execute() {								
				EntityListPresenterTest presenterTest = new EntityListPresenterTest();
				showWidget(presenterTest.getCanvas());				
			}
		};
		actionMap.put(entityListPresenter.getName(), entityListPresenter);
    	TreeNode node6 = new ActionTreeNode(entityListPresenter.getName(),entityListPresenter.getCaption());
    	treeNodes.add(node6);
    	
    	UserAction integerItemPresenter = new UserAction() {			
			@Override
			public String getName() {				
				return "integerItemPresenter";
			}
			
			@Override
			public String getCaption() {				
				return "Integer item Presenter";
			}
			
			@Override
			public void execute() {								
				IntegerItemPropertyPresenterTest presenterTest = new IntegerItemPropertyPresenterTest();
				showWidget(presenterTest.getCanvas());				
			}
		};
		actionMap.put(integerItemPresenter.getName(), integerItemPresenter);
    	TreeNode node7 = new ActionTreeNode(integerItemPresenter.getName(),integerItemPresenter.getCaption());
    	treeNodes.add(node7);
    	
    	UserAction booleanItemPresenter = new UserAction() {			
			@Override
			public String getName() {				
				return "booleanItemPresenter";
			}
			
			@Override
			public String getCaption() {				
				return "Boolean item Presenter";
			}
			
			@Override
			public void execute() {								
				CheckBoxItemPropertyPresenterTest presenterTest = new CheckBoxItemPropertyPresenterTest();
				showWidget(presenterTest.getCanvas());				
			}
		};
		actionMap.put(booleanItemPresenter.getName(), booleanItemPresenter);
    	TreeNode node8 = new ActionTreeNode(booleanItemPresenter.getName(),booleanItemPresenter.getCaption());
    	treeNodes.add(node8);
    	
    	UserAction entityEditor = new UserAction() {			
			@Override
			public String getName() {				
				return "entityEditor";
			}
			
			@Override
			public String getCaption() {				
				return "Entity Editor";
			}
			
			@Override
			public void execute() {								
				EntityArrayEditorTest presenterTest = new EntityArrayEditorTest();
				showWidget(presenterTest.getCanvas());				
			}
		};
		actionMap.put(entityEditor.getName(), entityEditor);
    	TreeNode node9 = new ActionTreeNode(entityEditor.getName(),entityEditor.getCaption());
    	treeNodes.add(node9);
    	//10
    	UserAction comboBox = new UserAction() {			
			@Override
			public String getName() {				
				return "comboBox";
			}
			
			@Override
			public String getCaption() {				
				return "ComboBox item";
			}
			
			@Override
			public void execute() {								
				ComboBoxItemPresenterTest presenterTest = new ComboBoxItemPresenterTest();
				showWidget(presenterTest.getCanvas());				
			}
		};
		actionMap.put(comboBox.getName(), comboBox);
    	TreeNode node10 = new ActionTreeNode(comboBox.getName(),comboBox.getCaption());
    	treeNodes.add(node10);
    	//11
    	UserAction timeEdit = new UserAction() {			
			@Override
			public String getName() {				
				return "TimeEdit";
			}
			
			@Override
			public String getCaption() {				
				return "Time edit";
			}
			
			@Override
			public void execute() {								
				TimeItemPropertyPresenterTest presenterTest = new TimeItemPropertyPresenterTest();
				showWidget(presenterTest.getCanvas());				
			}
		};
		actionMap.put(timeEdit.getName(), timeEdit);
    	TreeNode node11 = new ActionTreeNode(timeEdit.getName(),timeEdit.getCaption());
    	treeNodes.add(node11);
    	
    	//12
    	UserAction multiSelect = new UserAction() {			
			@Override
			public String getName() {				
				return "MultiSelect";
			}
			
			@Override
			public String getCaption() {				
				return "Multi select";
			}
			
			@Override
			public void execute() {								
				MultiSelectItemPresenterTest presenterTest = new MultiSelectItemPresenterTest();
				showWidget(presenterTest.getCanvas());				
			}
		};
		actionMap.put(multiSelect.getName(), multiSelect);
    	TreeNode node12 = new ActionTreeNode(multiSelect.getName(),multiSelect.getCaption());
    	treeNodes.add(node12);
    	
    	//13
    	UserAction groupedForm = new UserAction() {			
			@Override
			public String getName() {				
				return "GroupedForm";
			}
			
			@Override
			public String getCaption() {				
				return "Grouped Form";
			}
			
			@Override
			public void execute() {								
				GroupedFormEntityPresenterTest presenterTest = new GroupedFormEntityPresenterTest();
				showWidget(presenterTest.getCanvas());				
			}
		};
		actionMap.put(groupedForm.getName(), groupedForm);
    	TreeNode node13 = new ActionTreeNode(groupedForm.getName(),groupedForm.getCaption());
    	treeNodes.add(node13);
    	
    	//14
    	UserAction feedbackTest = new UserAction() {			
			@Override
			public String getName() {				
				return "FeedbackTest";
			}
			
			@Override
			public String getCaption() {				
				return "FeedbackTest";
			}
			
			@Override
			public void execute() {								
				FormFeedbackTest presenterTest = new FormFeedbackTest();
				showWidget(presenterTest.getCanvas());				
			}
		};
		actionMap.put(feedbackTest.getName(), feedbackTest);
    	TreeNode node14 = new ActionTreeNode(feedbackTest.getName(),feedbackTest.getCaption());
    	treeNodes.add(node14);
    	
    	//15
    	UserAction formTest = new UserAction() {			
			@Override
			public String getName() {				
				return "FormTest";
			}
			
			@Override
			public String getCaption() {				
				return "FormTest";
			}
			
			@Override
			public void execute() {								
				FormTest presenterTest = new FormTest();
				showWidget(presenterTest.getCanvas());				
			}
		};
		actionMap.put(formTest.getName(), formTest);
    	TreeNode node15 = new ActionTreeNode(formTest.getName(),formTest.getCaption());
    	treeNodes.add(node15);
    	
    	//16
    	UserAction selectorTest = new UserAction() {			
			@Override
			public String getName() {				
				return "SelectorTest";
			}
			
			@Override
			public String getCaption() {				
				return "Selector Test";
			}
			
			@Override
			public void execute() {								
				EntitySelectorTest presenterTest = new EntitySelectorTest();
				showWidget(presenterTest.getCanvas());				
			}
		};
		actionMap.put(selectorTest.getName(), selectorTest);
    	TreeNode node16 = new ActionTreeNode(selectorTest.getName(),selectorTest.getCaption());
    	treeNodes.add(node16);
    	
    	//17
    	UserAction popupEditorTest = new UserAction() {			
			@Override
			public String getName() {				
				return "PopupEditorTest";
			}
			
			@Override
			public String getCaption() {				
				return "Popup Editor";
			}
			
			@Override
			public void execute() {								
				TestPopupEntityEditor presenterTest = new TestPopupEntityEditor();
				showWidget(presenterTest.getCanvas());				
			}
		};
		actionMap.put(popupEditorTest.getName(), popupEditorTest);
    	TreeNode node17 = new ActionTreeNode(popupEditorTest.getName(),popupEditorTest.getCaption());
    	treeNodes.add(node17);
    }
    
   
    private Canvas createNavigationTree(){
        TreeGrid treeGrid = new TreeGrid();
        treeGrid.setWidth("30%");
        treeGrid.setShowConnectors(true);
        treeGrid.setShowResizeBar(true);

        Tree data = new Tree();
        //data.setModelType(TreeModelType.PARENT);
        data.setNameProperty("Name");
        data.setTitleProperty("Name");
        data.setIdField("actionId");
        
        data.setShowRoot(false);        
        data.setModelType(TreeModelType.PARENT);
        data.setData(getTreeNodes());       

        TreeGridField field = new TreeGridField("Navigation");
        field.setCellFormatter(new CellFormatter() {
            public String format(Object value, ListGridRecord record, int rowNum, int colNum) {
                return record.getAttribute("Name");
            }            
        });
        treeGrid.setFields(field);
        treeGrid.setData(data);
        treeGrid.addCellClickHandler(new CellClickHandler() {

            public void onCellClick(CellClickEvent event) {
                String actionId = event.getRecord().getAttribute("actionId");
                UserAction eventAction = actionMap.get(actionId);
                
                try {
                    if (eventAction != null){
                        eventAction.execute();
                    }
                    
                } catch (Exception ex) {
                    DefaultFeedbackPresenter presenter = new DefaultFeedbackPresenter();
                    presenter.showFeedback(ex.toString());
                }
            }
        });        

        return treeGrid;
    }


    public Widget getWidget(){
        return myHlayout;
    }

    private TreeNode[] getTreeNodes(){
    	TreeNode[] nodes = new TreeNode[treeNodes.size()];
    	treeNodes.toArray(nodes);
    	return nodes;
    }
    
    private void showWidget(Canvas widget){
    	displayStack.clear();
    	displayStack.setMembers(widget);    	
    	displayStack.draw();    	
    }
    
    public static class ActionTreeNode extends TreeNode {

        public ActionTreeNode(String actionId,  String name) {
            setActionId(actionId);            
            setName(name);             
        }

        public void setActionId(String value) {
            setAttribute("actionId", value);
        }

       @Override
        public void setName(String name) {
            setAttribute("Name", name);
        }

    }

}
